package com.edoomath.api.handler.impl;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.demo2do.core.utils.StringUtils;
import com.edoomath.api.handler.FileHandler;

@Component("fileHandlerImpl")
public class FileHandlerImpl implements FileHandler {
	
	@Value("#{cloud['cloud.host']}")
    private String cloudHost;
	
	@Value("#{cloud['cloud.path']}")
    private String cloudPath;

	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.handler.FileHandler#saveFile(org.springframework.web.multipart.MultipartFile)
	 */
	public String saveFile(MultipartFile multipartFile, String filename) throws IllegalStateException, IOException {
		
		String filePath = StringUtils.connect(cloudPath + "/task-files/" + filename);
		
		multipartFile.transferTo(new File(filePath));
		
		return org.apache.commons.lang3.StringUtils.replace(filePath, cloudPath, cloudHost);
	}
}
